package 力扣._739_每日温度;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

public class Solution2 {
    public int[] dailyTemperatures(int[] temperatures) {
        //单调栈
        Stack<Integer> stack = new Stack<>();
        int[] result = new int[temperatures.length];
        stack.push(0);
        for (int i = 1; i < temperatures.length; i++) {
            while (!stack.isEmpty() && (temperatures[stack.peek()] < temperatures[i])) {
                int t = stack.pop();
                result[t] = i-t;
            }
            stack.push(i);
        }
        return result;
    }
    public int[] dailyTemperatures2(int[] temperatures) {
        //单调栈
        Deque<Integer> stack = new LinkedList<>();
        int[] result = new int[temperatures.length];
        stack.push(0);
        for (int i = 1; i < temperatures.length; i++) {
            while (!stack.isEmpty() && (temperatures[stack.peek()] < temperatures[i])) {
                int t = stack.pop();
                result[t] = i-t;
            }
            stack.push(i);
        }
        return result;
    }

}
